﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

namespace _02.FilesAndFoldersTree
{
    class Folder
    {
        private string name;
        private List<File> files;
        private List<Folder> childFolders;

        public Folder(string name)
        {
            this.name = name;
            this.files = new List<File>();
            this.childFolders = new List<Folder>();
            FillTree(this);
        }

        public string Name
        {
            get { return this.name; }
        }

        public List<File> Files
        {
            get { return this.files; }
        }

        public List<Folder> ChildFolders
        {
            get { return this.childFolders; }
        }

        public long FilesSize
        {
            get
            {
                return this.files.Sum(x => x.Size);
            }
        }

        private void FillTree(Folder folder)
        {
            foreach (var name in Directory.GetFiles(folder.Name))
            {
                this.files.Add(new File(name));
            }

            foreach (var name in Directory.GetDirectories(folder.Name))
            {
                this.childFolders.Add(new Folder(name));
            }
        }

        public long CalculateTotalFilesSize()
        {
            long size = 0;

            size += this.files.Sum(x => x.Size);

            foreach (var subfolder in this.childFolders)
            {
                size += subfolder.CalculateTotalFilesSize();
            }

            return size;
        }
    }
}
